// my_other/dizhi/dizhi.js
let app = getApp()
let yuming = app.globalData.yuming;
let mobile = app.globalData.mobile
const request = require('../../config/request');
Page({

  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    title: '地址',
    buttonClicked: false,
    type: '',
    id: '',
    name: '',
    phone: '',
    shengguo: '请选择省',
    shiguo: '请选择市',
    quguo: '请选择区',
    shenglist: [],
    shilist: [],
    qulist: [],
    shengindex: 0,
    shiindex: 0,
    quindex: 0,
    pid: '', //省id
    cid: '', //市id
    quid: '', //区id
    checkValue: 0,
    isChecked: false
  },
  name(e) {
    this.setData({
      name: e.detail.value
    })
  },
  phone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  checkphone(e) {
    if (!mobile.test(e.detail.value)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      this.setData({
        phone: ''
      })
    }
  },
  // 省
  bindsheng(e) {
    var id = this.data.shenglist[e.detail.value].areaId
    this.setData({
      shengguo: this.data.shenglist[e.detail.value].areaName,
      pid: id,
      shiguo: '请选择市',
      cid: '',
      quguo: '请选择区',
      quid: '',
      shengindex: 0,
      shiindex: 0,
      quindex: 0,
    })
    this.getCity(id)
  },
  // 市
  bindshi(e) {
    var id = this.data.shilist[e.detail.value].areaId
    if (this.data.shengguo == '请选择省') {
      wx.showToast({
        title: '请选择省',
        icon: 'none'
      })
    } else {
      this.setData({
        shiguo: this.data.shilist[e.detail.value].areaName,
        cid: id,
        quguo: '请选择区',
        quid: '',
        shengindex: 0,
        shiindex: 0,
        quindex: 0,
      })
      this.getArea(id)
    }
  },
  // 区
  bindqu(e) {
    this.setData({
      quguo: this.data.qulist[e.detail.value].areaName,
      quid: this.data.qulist[e.detail.value].areaId,
      shengindex: 0,
      shiindex: 0,
      quindex: 0,
    })
  },
  // 获取省列表
  getProvince: function () {
    let that = this
    this.myRequest(yuming + 'Besall/areaList', {
      parentId: 0,
      areaType: 0,
    }, "POST").then(res => {
      that.setData({
        shenglist: res.data.data, //省级原始数据
      })
      let defaultCode = that.data.shenglist[0].areaId //使用第一项当作参数获取市级数据
      if (defaultCode) {
        that.getCity(defaultCode) //获取市区数据
      }
    })
  },
  //获取市列表
  getCity: function (id) {
    let that = this
    this.myRequest(yuming + 'Besall/areaList', {
      parentId: id,
      areaType: 1,
    }, "POST").then(res => {
      that.setData({
        shilist: res.data.data, //保持市级数据
      })
      let defaultCode = that.data.shilist[0].areaId //获取第一个市的区级数据
      if (defaultCode) {
        that.getArea(defaultCode) //获取区域数据
      }
      if (this.data.type == 2) {
        let cid = this.data.shilist.filter(item => item.areaName == that.data.shiguo)[0].areaId;
        this.getArea(cid)
      }
    })
  },
  //获取区列表
  getArea: function (id) {
    var that = this
    this.myRequest(yuming + 'Besall/areaList', {
      parentId: id,
      areaType: 2,
    }, "POST").then(res => {
      console.log(res)
      that.setData({
        qulist: res.data.data,
      })
    })
  },
  detailaddress(e) {
    this.setData({
      detailaddress: e.detail.value
    })
  },
  getDetail() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this;
    this.myRequest(yuming + 'Hallfood/userAddressInfo', {
      id: that.data.id
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading()
        console.log(res.data.data)
        let data = res.data.data
        this.setData({
          name: data.username,
          phone: data.mobile,
          shengguo: data.pid,
          shiguo: data.cid,
          quguo: data.did,
          detailaddress: data.address,
          checkValue: data.isDefault
        })
        if (this.data.checkValue == 0) {
          this.setData({
            isChecked: false
          })
        } else {
          this.setData({
            isChecked: true
          })
        }
        let pid = this.data.shenglist.filter(item => item.areaName == that.data.shengguo)[0].areaId;
        console.log(pid)
        this.getCity(pid)
      }
    })
  },
  duihuan() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    let url = '';
    let data = {};
    if (this.data.type == 1) {
      url = 'Hallfood/userAddressAdd';
      data = {
        shopId: wx.getStorageSync('shopId'),
        username: that.data.name,
        mobile: that.data.phone,
        pid: that.data.shengguo,
        cid: that.data.shiguo,
        did: that.data.quguo,
        address: that.data.detailaddress,
        isDefault: that.data.checkValue,
      };
    } else if (this.data.type == 2) {
      url = 'Hallfood/userAddressSave';
      data = {
        id: that.data.id,
        username: that.data.name,
        mobile: that.data.phone,
        pid: that.data.shengguo,
        cid: that.data.shiguo,
        did: that.data.quguo,
        address: that.data.detailaddress,
        isDefault: that.data.checkValue,
      }
    }
    this.myRequest(yuming + url, data, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1500)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  checkChange(e) {
    this.setData({
      isChecked: !this.data.isChecked
    })
    if (this.data.isChecked) {
      this.setData({
        checkValue: 1
      })
    } else {
      this.setData({
        checkValue: 0
      })
    }
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      type: options.type,
      id: options.id
    })
    this.getProvince(); //获取省
    if (this.data.type == 2) {
      this.getDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})